/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdFunctions.h"
#include <string.h>
#include "hhdAssert.h"

namespace hhd
{
    /**
    * Array1D encapsulates an array, includes a handy length lookup.  Can not be resized.
    */
    template <typename T>
	class Array1D
    {
    public:
        /**
        * Default constructor for Array1D allocates no space and sets internal data pointer to NULL.
        */
        Array1D()
        {
            _d1 = 0;
            _data = NULL;
        }

        /**
        * Explicit constructor defines the length of the array.
        *
        */

        explicit Array1D(size_t dimension1)
        {
            _d1 = dimension1;
			_data = _d1 ? new T[_d1] : NULL;
        }

		void setSize(size_t in_size)
		{
			if (in_size != _d1)
			{
				if (_d1)
				{
					delete _data;
				}

				_d1 = in_size;
				_data = new T[in_size];
			}
		}

        /**
        * Copy constructor for Array1D.  Deletes the current data held.
        */

        Array1D(const Array1D& other)
        {
            _d1 = other._d1;
			_data = _d1 ? new T[_d1] : NULL;
            for(size_t k = 0; k < _d1; k++)
            {
                _data[k] = other._data[k];
            }
        }

        /**
        * Destructor; deletes the internal array and sets to NULL
        */

        ~Array1D()
        {
            if(_data)
            {
                delete[] _data;
                _data = NULL;
            }
        }

        /**
        * Assignment operator, deletes the currently held data, news a new block a memory and
        * copies in other's data.
        */

        Array1D& operator=(const Array1D& other)
        {
            if(_data != NULL)
            {
                delete[] _data;
                _data = NULL;
            }

            _d1 = other._d1;

            if(_d1 > 0)
            {
                _data = new T[_d1];
                for(size_t k = 0; k < _d1; k++)
                {
                    _data[k] = other._data[k];
                }
            }
            return *this;
        }

        T operator()(size_t i) const
        {
            HHD_ASSERT(i >= 0 && i < _d1);
            return _data[i];
        }

        T& operator()(size_t i)
        {
            HHD_ASSERT(i >= 0 && i < _d1);
            return _data[i];
        }

        /** The size of the array **/
        inline size_t length() const
        {
            return _d1;
        }

        operator const T*() const
        {
            return _data;
        }

        operator T*()
        {
            return _data;
        }

    private:
        size_t _d1;
        T* _data;
    };
}